/*===== code build by mstore0.1.3 =====*/

/*===== mstoreDev/common#storeHeader:0 =====*/

$$(function($it){

    $it.listen('click', {
        createProject: function(){
            $$.action.openCreateProject();
        },
        openDir: function(){
            $$.action.openDir($(this).attr('path'));
        },
        viewDocs: function(){
            open('/@doc@/');
        },
        end: null
    });
    
});

/*===== mstoreDev/index#pageConfig:0 =====*/

$$(function($it){
    
    var hiddenProjectsKey = 'mstoreIndex.hiddenProjectIds';
    var hiddenProjectIds;
    
    $it.listen('click', {
        showStoreList: function(){
            $it.showStoreList($(this).parents('.projectItem'), true);
        },
        hideStoreList: function(){
            $it.hideStoreList($(this).parents('.projectItem'), true);
        }
    });
    
    $it.extend({
        domready: function(){
            $it.getHiddenProjectIds();
            $it.setProjectVisible();
        },
        getHiddenProjectIds: function(){
            hiddenProjectIds = $localStorege(hiddenProjectsKey);
            hiddenProjectIds = hiddenProjectIds ? ','+ hiddenProjectIds +',' : '';
            //$localStorege(hiddenProjectsKey, null);
            //console.info(hiddenProjectIds)
        },
        setProjectVisible: function(){
            $('.projectList .projectItem').each(function(){
                var projectId = $(this).attr('projectId');
                if(hiddenProjectIds.indexOf(','+ projectId +',') > -1){
                    $it.hideStoreList($(this));
                }else{
                    $it.showStoreList($(this));
                }
            });
        },
        showStoreList: function($projectItem, doSave){
            $projectItem.find('b[jhand="showStoreList"]').attr('jhand', 'hideStoreList').text('收起项目');
            $projectItem.find('.storeList').slideDown();
            if(doSave){
                $localStorege(
                    hiddenProjectsKey, 
                    ($localStorege(hiddenProjectsKey) || '').replace(','+ $projectItem.attr('projectId') + ',', '')
                );
            }
        },
        hideStoreList: function($projectItem, doSave){
            $projectItem.find('b[jhand="hideStoreList"]').attr('jhand', 'showStoreList').text('展开项目');
            $projectItem.find('.storeList').slideUp();
            if(doSave){
                $localStorege(
                    hiddenProjectsKey, 
                    ($localStorege(hiddenProjectsKey) || '') + ',' + $projectItem.attr('projectId') + ','
                );
            }
        }
    });
    
    $it.click({
        doEditor: function(){
            location.href = $(this).attr('storePath') + '.html?$edit=mstore';
        },
        addStoreTask: function(e){
            e.stopPropagation();
            var $storeItem = $(this).parents('.storeItem');
            $$.action.addStoreTask($storeItem.attr('storePath'), function(){
                $storeItem.addClass('assist');
            });
        }
    });
});
